package com.lej.theme.dialog

import android.content.Context
import com.lej.theme.R
import com.lej.theme.databinding.ThemeDialogEditBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.qmuiteam.qmui.kotlin.onClick

class CustomEditPopDialog(
    context: Context,
    var hint: String?,
    var confirmText: String = "确认",
    val onConfirm: (String) -> Unit = {}
) : CenterPopupView(context) {
    override fun getImplLayoutId() = R.layout.theme_dialog_edit
    override fun initPopupContent() {
        super.initPopupContent()
        val binding = ThemeDialogEditBinding.bind(centerPopupContainer.findViewById(R.id.cl_dialog))
        binding.edit.hint = hint

        binding.btnPositive.text = confirmText

        binding.btnPositive.onClick {
            onConfirm(binding.edit.text.toString())
            dismiss()
        }
        binding.ivIcon.onClick {
            dismiss()
        }
    }

    companion object {
        fun builder(
            context: Context,
            hint: String?,
            confirmText: String = "确认",
            onConfirm: (String) -> Unit = {}
        ) = XPopup.Builder(context)
            .dismissOnTouchOutside(false)
            .autoOpenSoftInput(true)
            .moveUpToKeyboard(true)   //是否移动到软键盘上面，默认为true
            .asCustom(
                CustomEditPopDialog(
                    context,
                    hint = hint,
                    confirmText = confirmText,
                    onConfirm = onConfirm
                )
            )
    }
}